<?php
class ReaderHelper
{
	static function getChapter($book_path, $pagesize=0)
	{
		import('#/helper/String.php');
		
		$file	=	BOOK_PATH.$book_path;
		if(!file_exists($file))
		{
			Show::error("<p>the book has been removed</p>");
		}
		
		$page	=	fetch_request('p', 'int', 1);
		$size	=	$pagesize ? $pagesize : app_cfg('setting', 'pagesize');
		$seek	=	($page-1)*$size;
		
		$fp		=	fopen($file, 'r');
		fseek($fp, $seek);
		$cont	=	fread($fp, $size+2);
		fclose($fp);
		
		return StringHelper::strcut($cont, $size);
	}
	
	static function getPager($book_size, $chapter, $pagesize=0)
	{
		$page	=	fetch_request('p', 'int', 1);
		$size	=	$pagesize ? $pagesize : app_cfg('setting', 'pagesize');
		$page_next	=	strlen($chapter)<$size-4 ? $page_next=$page : $page+1;
		$page_prior	=	$page-1;
		
		return array($page, $page_prior, $page_next, ceil($book_size/$size));
	}
	
	static function wmlFormat($str)
	{
		return str_replace(
				array('<','>','&','$',"\n"),  /*'\'','"',*/
				array('&lt;','&gt;','&amp;','$$','<br/>'), /*'&apos;','&quot;',*/
				$str);
	}
	static function htmlFormat($str)
	{
		return str_replace(
				array('<','>','&',"\n"),
				array('&lt;','&gt;','&amp;','<br/>'),
				$str);
	}
	
	static function anchor($bookid, $newanchor=null)
	{
//		$recently   =   fetch_cookie('rbooks', 'string');
//		if($recently)
//		{
//		    $recently   =   explode('-', $recently);
//		    $tmp    =   array();
//		    foreach($recently as $k=>$v)
//		    {
//		        $r  =   explode('.', $v);
//		        $tmp[$r[0]] =   array($r[1], $r[2]);
//		    }
//		    $recently   =   $tmp;
//		}
//		else
//		{
//		    $recently   =   array();
//		}
//
//		if($book_id===null)			//取全部读书记录
//		{
//		    return $recently;
//		}
//		elseif($page_num===null)    //删除某读书记录
//		{
//		    if(isset($recently[$book_id]))
//		    {
//		        unset($recently[$book_id]);
//		    }
//		}
//		elseif($page_num===0)       //取某读书记录 返回页数和时间
//		{
//		    return isset($recently[$book_id])?array($recently[$book_id]):array();
//		}
//		else                        //设置读书记录
//		{
//		    $recently[$book_id] =   array($page_num, time());
//		}
//
//		$c  =   array();
//		foreach($recently as $bid=>$p)
//		{
//		    $c[]    =  sprintf('%d.%d.%d', $bid, $p[0], $p[1]);
//		}
//
//		return esq_setcookie('rbooks', join('-', $c), '', 2592000, false);
		
	}
	
}
?>